package com.xhapi.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhapi.pojo.dto.ChoiceQuestionByAiDTO;
import com.xhapi.pojo.entity.ApiResult;
import com.xhapi.pojo.entity.ChoiceQuestionsByAi;
import com.xhapi.pojo.entity.HistoryQuestionByAi;
import com.xhapi.service.IChoiceQuestionsByAiService;
import com.xhapi.service.impl.ChoiceQuestionsByAiServiceImpl;
import com.xhapi.utils.ApiResultHandler;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 由知识库提供的单选题 前端控制器
 * </p>
 *
 * @author 179005
 * @since 2024-06-11
 */
@RestController
@RequestMapping("/choice-questions-by-ai")
@CrossOrigin
@Slf4j
@RequiredArgsConstructor
public class ChoiceQuestionsByAiController {
    private final IChoiceQuestionsByAiService service;

    // 传来的参数内包含了相关知识点 String， 具体用户ID Integer
    @ApiOperation("向知识库提问生成单选题，然后将结果返回数据库")
    @PostMapping("/ask")
    //public ApiResult ask(@RequestBody ChoiceQuestionByAiDTO dto){
    public ApiResult ask(@RequestParam Integer id,@RequestParam String pointList){
        ChoiceQuestionByAiDTO dto =new ChoiceQuestionByAiDTO();
        dto.setId(id);dto.setPointList(pointList);
        return ApiResultHandler.success(service.createQA(dto));
    }
    @ApiOperation("提交选择题答案并返回结果，同时更新数据库状态")
    @GetMapping("/ans")
    public ApiResult answer(@RequestParam Integer id,@RequestParam String answer,@RequestParam Integer userId){
        return ApiResultHandler.success(service.answer(id,answer,userId));
    }
    @ApiOperation("分页查询所有AI生成过的题目")
    @PostMapping("/pages")
    public ApiResult pages(@RequestParam(name = "userId", required = false) String userId,
                           @RequestParam(name = "pageSize", defaultValue = "6") int pageSize,
                           @RequestParam(name = "pageNum", defaultValue = "1") int pageNum){
        Page<ChoiceQuestionsByAi> result=service.lambdaQuery()
                .eq(ChoiceQuestionsByAi::getCreateUser,userId)
                .orderByAsc(ChoiceQuestionsByAi::getUpdateTime)
                .page(new Page<>(pageNum,pageSize));
        return ApiResultHandler.success(result);
    }
    @ApiOperation("根据id查询单个题目信息")
    @GetMapping("/{id}")
    public ApiResult getById(@PathVariable Integer id) {
        return ApiResultHandler.success(service.getById(id));
    }
}
